<template>
    <div class="role-wrap" style="padding: 6px">
        <el-card>
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
                <el-form-item label="时间">
                    <el-date-picker v-model="value1" type="datetimerange" range-separator="至" start-placeholder="开始日期"
                        end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss" @change="changeTime">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="车牌" prop="carPlate">
                    <el-input v-model="queryParams.plate_num" placeholder="请输入车牌" clearable style="width: 240px"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>

            </el-form>
        </el-card>
        <el-card class="main-card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">

                    <el-button type="success" icon="el-icon-upload" size="mini" @click="openImport">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table class="base-table" v-loading="loading" :data="roleList">
                <el-table-column label="序号" type="index" width="50">
                </el-table-column>
                <el-table-column label="车牌号" prop="plate_num" align="left" />
                <el-table-column label="车牌底色" prop="plate_color" align="left" />
                <el-table-column label="车牌识别时间" prop="start_time" align="left">
                    <template slot-scope="scope">
                        <div>{{ timestampToDate(scope.row.start_time * 1000) }}</div>
                    </template>
                </el-table-column>
                <el-table-column label="相机ip" align="center" width="100" prop="cam_ip">

                </el-table-column>


                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button size="small" type="primary" plain style="padding: 5px" icon="el-icon-view"
                            @click="handleViewquan(scope.row)">查看全图</el-button>
                        <el-button size="small" plain style="padding: 5px" icon="el-icon-view"
                            @click="handleViewquan2(scope.row)">查看局部</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 添加或修改角色配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <div style="width: 200px;height: 200px;">
                <img :src="quanPicture" alt="" srcset="">
            </div>


        </el-dialog>




    </div>
</template>

<script>
import { getCarRecordLst, exportCarRecordLst } from "@/api/car/car";
import axios from 'axios';
import { getToken } from '@/utils/auth'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
    dicts: ['sys_normal_disable'],
    components: { Treeselect },
    data() {
        return {
            // 遮罩层
            loading: true,
            dialogVisible: false,

            // 总条数
            total: 0,
            // 角色表格数据
            roleList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,


            // 查询参数
            queryParams: {
                plate_num: "",
                endTime: "",
                startTime: "",
                pageNum: 1,
                pageSize: 10,

            },
            // 表单参数
            form: {},

            // 表单校验
            rules: {
                carCode: [
                    { required: true, message: "车辆属性不能为空", trigger: "change" }
                ],
                whitelistIp: [
                    { required: true, message: "白名单不能为空", trigger: "blur" }
                ],
                carPlate: [
                    { required: true, message: "车牌号不能为空", trigger: "blur" }
                ],
                cmpName: [
                    { required: true, message: "归属单位/个人名称不能为空", trigger: "blur" }
                ],
                carType: [
                    { required: true, message: "车辆类型不能为空", trigger: "change" }
                ],
                status: [
                    { required: true, message: "车辆类型不能为空", trigger: "change" }
                ],
                start: [
                    { required: true, message: "车辆通行开始时间不能为空", trigger: "change" }
                ],
                end: [
                    { required: true, message: "车辆通行结束时间不能为空", trigger: "change" }
                ],
                //start
            },
            deviceList: [],
            quanPicture: "",
            value1: []
        };
    },
    created() {
        this.getList();
    },
    methods: {
        changeTime(e) {
            this.queryParams.startTime = e[0]
            this.queryParams.endTime = e[1]
        },

        openImport() {
            if (this.value1.length == 0) {
                this.$message({
                    message: '请选择时间',
                    type: 'warning'
                });
                return
            }
            delete this.queryParams.pageNum;
            delete this.queryParams.pageSize;
            exportCarRecordLst(this.queryParams).then((res) => {

            })
        },

        timestampToDate(timestamp) {
            var date = new Date(timestamp); // 转换为Date对象

            // 获取年月日时分秒
            var year = date.getFullYear();
            var month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份是从0开始的
            var day = ('0' + date.getDate()).slice(-2);
            var hours = ('0' + date.getHours()).slice(-2);
            var minutes = ('0' + date.getMinutes()).slice(-2);
            var seconds = ('0' + date.getSeconds()).slice(-2);

            // 组合成 yyyy-mm-dd HH:mm:ss 格式
            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        },

        handleViewquan(row) {
            this.open = true
            this.title = "查看"
            this.quanPicture = row.picture;
        },


        handleViewquan2(row) {
            this.open = true
            this.title = "查看"
            this.quanPicture = row.closeup_pic;
        },

        //下载模板
        handleImport() {
            let obj = {
                fileName: "车辆信息上传模板-车牌.xlsx"
            }
            // downloadCarTempate(obj).then(res => {
            //     console.log(res);

            // })
            this.download2('system/car_details/template/downLoad', { obj }, `车辆信息上传模板-车牌.xlsx`)
            // axios.get('/dev-api/system/car_details/template/downLoad', {
            //     params: {
            //         fileName: '车辆信息上传模板-车牌.xlsx'
            //     },
            //     headers: {
            //         "Authorization": 'Bearer ' + getToken(),
            //         "Content-type":"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
            //     },

            // }, { responseType: 'blob' }).then(res => {
            //     console.log(res)
            // })


            axios.get('/dev-api/system/car_details/template/downLoad', {
                params: {
                    fileName: "车辆信息上传模板-车牌.xlsx",
                },
                headers: {
                    'Authorization': 'Bearer ' + getToken()
                },
            }, { responseType: 'blob' })
                .then((response) => {
                    console.log(response);

                    const url = window.URL.createObjectURL(new Blob([response.data]));
                    const link = document.createElement('a');
                    link.href = url;
                    link.setAttribute('download', '车辆信息上传模板-车牌.xlsx');
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                })
                .catch((error) => {
                    console.error(error);
                });
            // this.download2('system/car_details/template/downLoad',obj, `车辆信息上传模板-车牌.xlsx`)
        },
        /** 查询角色列表 */
        getList() {
            this.loading = true;
            getCarRecordLst(this.queryParams).then(response => {
                this.roleList = response.data.list;
                this.total = response.data.total;
                this.loading = false;
            }
            );
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },

        // 表单重置
        reset() {
            this.form = {}
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {

            this.resetForm("queryForm");
            this.handleQuery();
        },

        /** 新增按钮操作 */
        handleAdd() {
            // this.reset();
            this.open = true;
            this.title = "添加";
            this.isEdit = true;
            this.getWhiteCarListFun()

        },

        getWhiteCarListFun() {
            getWhiteCarList().then((res) => {
                if (res.code == 0) {
                    this.deviceList = res.data
                }

            })
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            console.log(row);
            this.getWhiteCarListFun()
            row.status = (row.status).toString()
            row.whitelistIp = row.whitelistIp.split(",")
            this.reset();
            this.form = row;
            this.open = true;
            this.title = "编辑";

        },

        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.form.whitelistIp = (this.form.whitelistIp).toString()
                    if (this.form.id) {

                        editCar(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {

                        addCar(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 提交按钮（数据权限） */
        submitDataScope: function () {
            if (this.form.roleId != 0) {
                this.form.deptIds = this.getDeptAllCheckedKeys();
                dataScope(this.form).then(response => {
                    this.$modal.msgSuccess("修改成功");
                    this.openDataScope = false;
                    this.getList();
                });
            }
        },
        /** 删除按钮操作 */
        handleDelete(row) {

            this.$modal.confirm('是否确认删除?').then(function () {
                return deleteCar(row.id);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/role/export', {
                ...this.queryParams
            }, `role_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
<style>
.role-wrap {
    padding: 6px;
}

.top-card-wrap {
    margin-bottom: 5px;
}

.card-wrap {
    padding-bottom: 100px;
}
</style>